package com.base.study.presenter;

import android.app.Activity;

import com.base.commonlib.base.presenter.BasePresenter;
import com.base.commonlib.http.RealNetWorkUtil;
import com.base.commonlib.http.utils.CallBackImpl;
import com.base.study.bean.EsSearchResultBean;
import com.base.study.bean.SearchResultBean;
import com.base.study.view.PopualSearchView;

import java.util.HashMap;
import java.util.List;

import io.reactivex.disposables.Disposable;

/**
 * Created by weikailiang on 2020/3/4.
 */

public class PopualSearchPresenter extends BasePresenter<PopualSearchView>{
    public PopualSearchPresenter(PopualSearchView view, Activity activity) {
        super(view, activity);
    }
    public void getPopueSearchList(final Integer index, String keyWord){
        HashMap<String, Object> params = new HashMap<>();
        params.put("page", index);
        params.put("limit",15);
        params.put("keyWord",keyWord);
        //初始化控件
        Disposable disposable = RealNetWorkUtil.postContent()
                .content(params)
                .method("api-train/app/search")
                .build().enqueueForArray(SearchResultBean.class, new CallBackImpl<List<SearchResultBean>>() {
                    @Override
                    public void onError(int code, String msg, String errMsg) {
                        mView.showError();
//                        mView.returnError();

                    }
                    @Override
                    public void onNext(List<SearchResultBean> list) {
                        if (index==1){
                            //刷新
                            if (list!=null &&list.size()>0){
                                mView.returnList(list);
                            }else {
                                mView.returnNoData();
                            }
                        }else {
                            //更多
                            if (list!=null &&list.size()>0){
                                mView.returnMoreList(list);
                            }else {
                                mView.returnNoMoreData();
                            }
                        }
                    }
                });
        mCompositeDisposable.add(disposable);
    }




    public void getSearchList(final Integer index,final Integer pageSize, String keyWord){
        HashMap<String, Object> params = new HashMap<>();
        params.put("pageNum", index);
        params.put("pageSize", pageSize);
        params.put("keyword",keyWord);
        //初始化控件
        Disposable disposable = RealNetWorkUtil.get()
                .params(params)
                .method("api-es/es/searchAll")
                .build().enqueue(EsSearchResultBean.class, new CallBackImpl<EsSearchResultBean>() {
                    @Override
                    public void onError(int code, String msg, String errMsg) {
                        mView.showError();
//                        mView.returnError();

                    }
                    @Override
                    public void onNext(EsSearchResultBean list) {
                        if (index==1){
                            //刷新
                            if (list!=null &&list.getList().size()>0){
                                mView.returnEsList(list);
                            }else {
                                mView.returnNoData();
                            }
                        }else {
                            //更多
                            if (list!=null &&list.getList().size()>0){
                                mView.returnEsList(list);
                            }else {
                                mView.returnNoMoreData();
                            }
                        }
                    }
                });
        mCompositeDisposable.add(disposable);
    }
}
